<template>
  <!-- 轮播配置页面 -->
  <div>
    <!-- 头部按钮 -->
    <div class="header">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="toAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">预览</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 头部按钮结束 -->
    <!-- 表格 -->
    <el-table :data="cloumns" style="width: 100%">
      <el-table-column type="index" :index="1" label="序号" width="180" />
      <el-table-column prop="name" label="轮播图名称" width="250" />
      <el-table-column prop="introduce" label="轮播图描述" width="250" />
      <el-table-column prop="url" label="轮播图图片" width="250">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <img
              slot="reference"
              :src="scope.row.url"
              width="30px"
              alt="加载失败"
            >
            <img width="200px" :src="scope.row.url" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '正常'"
            type="success"
          >正常</el-tag>
          <el-tag v-if="scope.row.status === '停用'" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click.prevent="toEdit(scope.row)">编辑</a>
          <a
            href="#"
            class="toremove"
            @click.prevent="toDelect(scope.row.id)"
          >删除</a>
          <a
            v-if="scope.row.status === '正常'"
            href="#"
            class="onLine"
            @click.prevent="changeStatus(scope.row, '停用')"
          >停用</a>
          <a
            v-else
            href="#"
            class="onLine"
            @click.prevent="changeStatus(scope.row, '正常')"
          >启用</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :rules="rules" :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <!-- 封面 -->
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.introduce" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
    <!-- 预览模态框 -->
    <el-dialog :title="title" :visible.sync="visible1" width="80%" @close="toClose">
      <el-carousel :interval="5000" arrow="always" height="300px">
        <el-carousel-item v-for="item in cloumns" :key="item.id">
          <img :src="item.url" alt="图片加载失败">
        </el-carousel-item>
      </el-carousel>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toSubmit">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 预览模态框结束 -->
  </div>
</template>

<script>
import { findAll, saveOrUpdate, deleteById } from '../../../api/carousel'
import { uploadFileURL } from '../../../utils/config'
export default {
  data() {
    return {
      params: {},
      cloumns: [],
      title: '',
      rules: {
        name: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' }
        ],
        url: [{ required: true, message: '请上传轮播图', trigger: 'change' }]
      },
      visible: false,
      form: {},
      formLabelWidth: '120px',
      uploadFileURL,
      // 头像上传图片变量
      imageUrl: '',
      visible1: false
    }
  },
  computed: {},

  created() {
    this.getCarousel()
  },
  mounted() {},
  methods: {
    // 删除
    // 删除
    toDelect(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteById({ id: id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getCarousel()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 编辑
    toEdit(row) {
      // console.log(row);
      this.title = '编辑轮播图'
      this.visible = true
      this.form = { ...row }
      this.imageUrl = this.form.url
    },
    // 图片上传成功的方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.url = res.data
    },
    // 图片上传之前的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 模态框关闭回调函数
    toClose() {
      // 重置表单
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 保存或更新
    toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或者更新请求
          const res = await saveOrUpdate(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getCarousel()
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 新增
    toAdd() {
      this.title = '新增轮播图'
      this.visible = true
      this.form = {}
    },
    // 预览
    onSearch() {
      this.title = '大屏预览'
      this.visible1 = true
    },
    // 获取轮播图数据
    async getCarousel() {
      const res = await findAll()
      console.log(res)
      this.cloumns = res.data
    },
    // 执行启用和停用
    async  changeStatus(row, status) {
      if (status === '停用') {
        row.status = '停用'
      } else {
        row.status = '正常'
      }
      console.log(row)
      const res = await saveOrUpdate(row)
    }
  }
}
</script>
<style lang="scss">
.toEdit {
  color: #30b08f;
}
.toremove {
  color: #e65d6e;
}
.onLine {
  color: #3a71a8;
}
</style>
